package com.china08.yxyapi.api.manage.stu.mystudy.homework.dowork;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;

/**
 * 家长端作业系统 班级作业（试题）页面
 * 
 * @author yangyunying
 *
 */
@CrossOrigin
@RestController
public class DoWorkNoFinishedCountFindController extends YxyApiBaseController {

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/dowork/nofinishedcount/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        Date currentDate = new Date();
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        // 要查询的家长孩子Id
        String studentId = reqModel.studentId;
        // 全部
        Integer allCount = homeworkTaskReportRepository
                .countByStudentIdAndStatusOfFinishedAndPredictDateGreaterThanEqualAndPublishDateLessThanEqual(studentId,
                        0, currentDate, currentDate);
        RespModel respModel = new RespModel();
        respModel.courseName = "全部";
        respModel.count = allCount;
        respModelLst.add(respModel);

        // 语文 数学 英语 物理 化学 生物 政治 历史 地理
        String[] subjectNameArr = new String[] { "语文", "数学", "英语", "物理", "化学", "生物", "政治", "历史", "地理" };
        List<String> subjectNameLst = Arrays.asList(subjectNameArr);
        // 每个学科的数量
        Integer oneCount = 0;
        // 已知科目累加数量
        Integer countAdd = 0;
        for (String subjectName : subjectNameLst) {
            respModel = new RespModel();
            oneCount = homeworkTaskReportRepository
                    .countByStudentIdAndStatusOfFinishedAndSubjectNameAndPredictDateGreaterThanEqualAndPublishDateLessThanEqual(
                            studentId, 0, subjectName, currentDate, currentDate);
            countAdd += oneCount;
            respModel.courseName = subjectName;
            respModel.count = oneCount;
            respModelLst.add(respModel);
        }

        // 其他
        Integer qitaCount = 0;
        if (allCount >= countAdd) {
            qitaCount = allCount - countAdd;
        } else {
            qitaCount = 0;
        }
        respModel = new RespModel();
        respModel.courseName = "其他";
        respModel.count = qitaCount;
        respModelLst.add(respModel);
        return respModelLst;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 学生id
        public String studentId;
    }

    protected static class RespModel extends YxyApiRespModel {
        /**
         * 学科名称
         */
        public String courseName;
        /**
         * 数量
         */
        public int count;
    }
}
